/**
 * Created by shenhai on 15/9/25.
 */

var TopicService = require('../../../services/mis/operate/topicService');
var messageUtil = require('../../../common/utils/messageUtil');
var toolUtil = require('../../../common/utils/toolUtil');


exports.topicList = function(req, res) {

    TopicService.topicListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when topicList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when topicList...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};

exports.topicAdd = function(req, res) {

    //if (req.body.image1) {
    //
    //    toolUtil.uploadImageService(null, null, req, function (err, images) {
    //        if (err) {
    //            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
    //        } else {
    //            if (images.length > 0) { req.body.imageURL = images[0];}
    //            if (images.length > 1) { }
    //            delete req.body.image1;
    //            delete req.body.image2;
    //
    //            TopicService.topicAddService(req, function(err, results) {
    //
    //                if (err) {
    //                    logger.error('Exception happen when topicAdd...' + err.message);
    //                    res.json(messageUtil.composeFailedMessage(req, 'Exception happen when topicAdd...'));
    //                }else {
    //                    res.json(messageUtil.composeSucceededMessage(req, null, {}));
    //                }
    //            });
    //        }
    //    });
    //
    //}else {

        TopicService.topicAddService(req, function(err, results) {

            if (err) {
                logger.error('Exception happen when topicAdd...' + err.message);
                res.json(messageUtil.composeFailedMessage(req, 'Exception happen when topicAdd...'));
            }else {
                res.json(messageUtil.composeSucceededMessage(req, null, {}));
            }
        });
    //}
};



exports.topicEdit = function(req, res) {


    //if (req.body.image1) {
    //
    //    toolUtil.uploadImageService(null, null, req, function (err, images) {
    //        if (err) {
    //            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
    //        } else {
    //            if (images.length > 0) { req.body.imageURL = images[0];}
    //            if (images.length > 1) { }
    //            delete req.body.image1;
    //            delete req.body.image2;
    //
    //            TopicService.topicEditService(req, function(err, results) {
    //
    //                if (err) {
    //                    logger.error('Exception happen when topicEdit...' + err.message);
    //                    res.json(messageUtil.composeFailedMessage(req, 'Exception happen when topicEdit...'));
    //                }else {
    //                    res.json(messageUtil.composeSucceededMessage(req, null, {}));
    //                }
    //            });
    //        }
    //    });
    //
    //}else {

        TopicService.topicEditService(req, function(err, results) {

            if (err) {
                logger.error('Exception happen when topicEdit...' + err.message);
                res.json(messageUtil.composeFailedMessage(req, 'Exception happen when topicEdit...'));
            }else {
                res.json(messageUtil.composeSucceededMessage(req, null, {}));
            }
        });
    //}
};






exports.topicStatus = function(req, res) {

    TopicService.topicStatusService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when topicStatus...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when topicStatus...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, {}));
        }

    });
};



exports.topicDelete = function(req, res) {

    TopicService.topicDeleteService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when topicDelete...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when topicDelete...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, {}));
        }
    });
};
